package com.innazis.jfrogsurvey.config;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Inna on 12.07.14.
 */
public class CommonUtilities {
    public static SharedPreferences sPref;

    private static CommonUtilities instance;
    private Context context;

    private CommonUtilities() {
    }

    /**
     * Constructor
     *
     * @param context
     * @return
     */
    public static CommonUtilities getInstance(Context context) {
        if (instance == null) {
            instance = new CommonUtilities();
            instance.context = context;
        }
        return instance;
    }

    // ============ FUNCTIONS =============== //

    public boolean isRegisteredSharedPref(Context context){
        boolean result = false;
        sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
        String savedText = sPref.getString(Constants.USER, "");
        if (savedText != "" && savedText != null)
            result = true;
        return result;
    }

    public boolean savePreverences(Context context, String prefName,
                                   String prefValue) {
        boolean result = false;
        sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
        SharedPreferences.Editor ed = sPref.edit();
        ed.putString(prefName, prefValue);
        result = ed.commit();
        return result;
    }

    public void putStringValue(String key, String s) {
        SharedPreferences pref = context.getSharedPreferences(
                Constants.PREFERENCES_FILENAME, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, s);
        editor.commit();
    }
    public String pullPreferences(Context context, String prefName) {
        sPref = context.getSharedPreferences(Constants.PREFERENCES_FILENAME, 0);
        String savedText = sPref.getString(prefName, "");
        return savedText;
    }
}
